/*
 * @Author: humengchuan 531537052@qq.com
 * @Date: 2025-01-04 21:20:44
 * @LastEditors: humengchuan 531537052@qq.com
 * @LastEditTime: 2025-01-04 21:20:55
 * @FilePath: \project-my\vue-mall\src\utils\bus.ts
 * @Description: 自定义bus
 */
/**
 * @description: Bus总线传递
 * @return {*}
 */

type BusClass = {
    emit: (name: string) => void

    on: (name: string, callback: Function) => void
}

type PramsKey = string | number | symbol

type List = {
    [key: PramsKey] : Array<Function>
}

class Bus implements BusClass {
    list: List
    constructor () {
        this.list = {}
    }
    emit (name: string, ...args: Array<any>) {
        let eventName: Array<Function> = this.list[name]
        eventName.forEach(fn => {
            fn.apply(this, args)
        })
    }
    on (name: string, callback: Function) {
        let fn: Array<Function> = this.list[name] || []
        fn.push(callback)
        this.list[name] = fn
    }
}

export default new Bus()